/*******************************************************************************
 *  Dice Roller 2 is a tabletop rpg dice roll utility tool
 *     Copyright (C) 2014, 2015 David Meersteiner
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *     
 *     Contact me under:
 *     
 *     dmeersteiner@gmail.com
 *     
 *     David Meersteiner
 *     Am Hang 10
 *     94253 Bischofsmais
 *     GERMANY
 *******************************************************************************/
package de.dm.dr2.data.consolemessages;

import java.util.Map;

import de.dm.dr2.data.diceexpressions.DiceExpression;
import de.dm.dr2.data.util.*;

/**
 * 
 * A statistics {@code DiceExpression} collection.
 * 
 * @author D. Meersteiner
 * @see DiceExpression
 */
public class StatisticsMessage extends ConsoleMessage {
	
	/**
	 * The whole generated formatted Message 
	 */
	private String message;
	
	private CollectionReturn collection;
	
	/**
	 * <p>Creates a new formated message from a {@code DiceExpression}.
	 * 
	 * <p>When the {@code DiceRollMessage} is created, a {@code RollReturn} gets
	 * generated by {@link DiceExpression#roll()} and is used in the message.
	 * @author D. Meersteiner
	 * @param expression - the {@code DiceExpression} that will be formatted and rolled
	 * @see DiceExpression
	 */
	public StatisticsMessage(DiceExpression expression) {
		collection = expression.collect();
		message = "Statistics of "+expression.toString()+":"
				+Constants.NEW_LINE
				+getRollMessage()
				+getStatisticMessage()
				+Constants.NEW_LINE;
	}
	
	@Override
	public String getMessage() {
		return message;
	}

	@Override
	public String getValue() {
		return buildStats(collection, "; ");
	}

	/**
	 * @return the rollMessage
	 */
	public String getRollMessage() {
		return "Rolled: "
				+collection.getMessage()
				+Constants.NEW_LINE;
	}

	/**
	 * @return the statisticMessage
	 */
	public String getStatisticMessage() {
		return "Statistics: "
				+buildStats(collection)
				+Constants.NEW_LINE;
	}

	/**
	 * Formatted for PnlStatistics
	 * @return the statisticMessage
	 */
	public String getStatisticMessageWithNewLine() {
		return "Statistics: "+Constants.NEW_LINE+buildStatsNewLine(collection)+Constants.NEW_LINE;
	}
	
	private static String buildStats(CollectionReturn collection) {
		return buildStats(collection, "; ");
	}
	
	private static String buildStatsNewLine(CollectionReturn collection) {
		return buildStats(collection, Constants.NEW_LINE);
	}
	
	/**
	 * Builds a message from the rolled values hold by the {@code collection}
	 * @param collection
	 * @param elementDivider the divider between two statistics
	 * @return
	 */
	private static String buildStats(CollectionReturn collection, String elementDivider) {
		StringBuilder sb = new StringBuilder();
		Double lastKey = collection.lastKey();
		
		//Build message
		for (Map.Entry<Double, Integer> entry : collection.entrySet()) {
			Double key = entry.getKey();
			sb.append(UtilFunction.doubleToStringWithMinimumPrecision(key)); // the rolled value
			sb.append(": ");
			sb.append(String.format("%s", entry.getValue())); // the times
			sb.append(Constants._statisticsTimes);
			if (key != lastKey) {
				sb.append(elementDivider);
			}
		}
		return sb.toString();
	}
	
}
